package com.aica.controller;

import com.aica.service.UserService;
import com.aica.vo.request.*;
import com.aica.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

/**
 * 用户信息及相关操作接口
 * @author by
 * @date 2021/6/9 22:06
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping(value = "/email/send")
    @PreAuthorize("permitAll()")
    public ResultVo<Object> emailSend(@RequestBody @Valid SendEmailCodeVo sendEmailCodeVo){
        return userService.sendEmail(sendEmailCodeVo);
    }


    @PostMapping(value = "/register")
    @PreAuthorize("permitAll()")
    public ResultVo<Object> register(@RequestBody @Valid RegisterVo registerVo){
        return userService.register(registerVo);
    }


    @PostMapping(value = "/password/change/old")
    @PreAuthorize("isAuthenticated()")
    public ResultVo<Object> changePasswordByOld(
            @RequestBody @Valid ChangePasswordByOldVo changePasswordByOldVo,
            UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken){

        return userService.changePasswordByOld(changePasswordByOldVo,usernamePasswordAuthenticationToken);
    }

    @GetMapping(value = "/information/select")
    @PreAuthorize("isAuthenticated()")
    public ResultVo<Object> getUserInformation(
            UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken){

        return userService.getUserInformation(usernamePasswordAuthenticationToken);
    }

    @PostMapping(value = "/information/update")
    @PreAuthorize("isAuthenticated()")
    public ResultVo<Object> updateUserInformation(
            @RequestBody @Valid UpdateInformationVo updateInformationVo,
            UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken){

        return userService.updateInformation(updateInformationVo,usernamePasswordAuthenticationToken);
    }

    @PostMapping(value = "/upload/avatar")
    @PreAuthorize("isAuthenticated()")
    public ResultVo<Object> refreshAvatar(
            /*@RequestBody @Valid*/ UploadAvatarVo uploadAvatarVo,
            UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken){

        return userService.refreshAvatar(uploadAvatarVo,usernamePasswordAuthenticationToken);
    }

//    //测试权限不足拦截
//    @PostMapping(value = "/test")
//    @PreAuthorize("hasAuthority('USER_2')")
//    public ResultVo<Object> test(
//            UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken){
//        return ResultConstant.OK;
//    }

}
